package seu.evaluation.metric;

import java.text.DecimalFormat;
import java.util.Date;
/**
 * 
 * @author samantha
 * 可维护性子属性度量类，父类
 * 子类需要实现抽象方法run来进行具体度量
 * 子类包括lCC、TCC、CCN、FFC、CBO、RFC
 * getKind():返回当前度量属性的名称
 * metricMethod():具体的度量方法、公式
 * compomemtValueMetric(): 对每个组件的度量
 * finalValueMetric():整体度量结果
 *
 */
public abstract class MaintainabilityMetricStrategy {
	MetricResult result=new MetricResult(this.getType(),new Date());
	MaintainabilityDiagram umlDiagramInfo;
	
	protected abstract String getType();
	
	protected abstract double differentMetricMethod(MaintainabilityEntity entity);
	
	protected abstract void preprocess();
	
	protected void metric(){
		preprocess();
	
		DecimalFormat f = new DecimalFormat("#0.00");
		double totalValue=0;
		double totalComponent = 0;
		for(MaintainabilityEntity entity: umlDiagramInfo.getListEntity()){		
			if( !umlDiagramInfo.getExternalEntity().contains( entity.getComponentID() ) )
				continue;
			double value = (double)differentMetricMethod(entity);
			if( value >=0 ){
				totalComponent ++; 
				totalValue+=(double) value;
				result.addSubResult(entity.getName(), f.format(value));
			}
			else
				result.addSubResult(entity.getName(), "not applied");
			
		}
		result.setFinalResult( f.format(totalValue/ totalComponent));
	}
	
	public MetricResult run(MaintainabilityDiagram diagram){
		umlDiagramInfo=diagram;
		metric();	
		return result;
	}
}
